using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;

namespace Elephant.Framework.Services
{
    public class SceneComponent : DrawableGameComponent, ISceneService
    {
        ComponentManager manager;
        EntityFactory factory;

        public SceneComponent(Game game)
            : base(game)
        {
            if (game.Services.GetService(typeof(ISceneService)) != null) {
                throw new ArgumentException("SceneComponent already present.");
            }

            game.Services.AddService(typeof(ISceneService), this);

            manager = new ComponentManager();
            factory = new EntityFactory(manager);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing) {
                if (this.Game.Services.GetService(typeof(ISceneService)) == this) {
                    this.Game.Services.RemoveService(typeof(ISceneService));
                }
            }

            base.Dispose(disposing);
        }

        public override void Initialize()
        {
            manager.Initialize();

            base.Initialize();
        }

        protected override void LoadGraphicsContent(bool loadAllContent)
        {
            manager.LoadGraphicsContent(loadAllContent);
        }

        protected override void UnloadGraphicsContent(bool unloadAllContent)
        {
            manager.UnloadGraphicsContent(unloadAllContent);
        }

        public override void Update(GameTime gameTime)
        {
            manager.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            manager.Draw(gameTime);
        }
        
        #region IEntityManagerService Members

        public ComponentManager Manager
        {
            get
            {
                return manager;
            }
        }

        public EntityFactory Factory
        {
            get 
            { 
                return factory; 
            }
        }

        #endregion
    }
}
